package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.CallSearchItemsDTO;
import com.xbongbong.paas.pojo.dto.CallTaskLogBatchDeleteDTO;
import com.xbongbong.paas.pojo.dto.CallTaskLogCountDTO;
import com.xbongbong.paas.pojo.dto.CallTaskLogDeleteDTO;
import com.xbongbong.paas.pojo.dto.CallTaskLogFindNextPhoneDTO;
import com.xbongbong.paas.pojo.dto.CallTaskLogListDTO;
import com.xbongbong.paas.pojo.dto.FeeRemainDTO;
import com.xbongbong.paas.pojo.vo.CallSearchItemsVO;
import com.xbongbong.paas.pojo.vo.CallTaskLogBatchDeleteVO;
import com.xbongbong.paas.pojo.vo.CallTaskLogCountVO;
import com.xbongbong.paas.pojo.vo.CallTaskLogDeleteVO;
import com.xbongbong.paas.pojo.vo.CallTaskLogFindNextPhoneVO;
import com.xbongbong.paas.pojo.vo.CallTaskLogVO;
import com.xbongbong.paas.pojo.vo.FeeRemainVO;
import com.xbongbong.paas.service.XbbCallTaskLogService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author youli.chen
 * @version v1.0
 * @description: 批量外呼计划日志接口
 * @date 2019/2/25 15:33
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/callCenter/callTaskLog")
public class XbbCallTaskLogController {

    @Resource
    private XbbCallTaskLogService xbbCallTaskLogService;

    /**
     * 批量外呼计划日志列表
     *
     * @return 批量外呼计划日志列表
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String callTaskLogList(@RequestBody @Valid CallTaskLogListDTO callTaskLogListDTO, BindingResult br) {
        XbbResponse<CallTaskLogVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CallTaskLogVO callTaskLogVO = this.xbbCallTaskLogService.findDetails(callTaskLogListDTO);
                response = new XbbResponse<>(callTaskLogVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 批量外呼计划日志单个删除
     *
     * @return 批量外呼计划日志单个删除
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid CallTaskLogDeleteDTO callTaskLogDeleteDTO, BindingResult br) {
        XbbResponse<CallTaskLogDeleteVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.xbbCallTaskLogService.delete(callTaskLogDeleteDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 批量删除(结束上一次的批量外呼任务)
     *
     * @return 批量删除(结束上一次的批量外呼任务)
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String batchDelete(@RequestBody @Valid CallTaskLogBatchDeleteDTO callTaskLogBatchDeleteDTO, BindingResult br) {
        XbbResponse<CallTaskLogBatchDeleteVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.xbbCallTaskLogService.batchDelete(callTaskLogBatchDeleteDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取下一个需要拨打的号码(模糊查询出号码明文)
     *
     * @return 获取下一个需要拨打的号码(模糊查询出号码明文)
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/nextPhone", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String findNextPhone(@RequestBody @Valid CallTaskLogFindNextPhoneDTO callTaskLogFindNextPhoneDTO, BindingResult br) {
        XbbResponse<CallTaskLogFindNextPhoneVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.xbbCallTaskLogService.findNextPhone(callTaskLogFindNextPhoneDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取每个通话状态对应的总条数
     *
     * @return 获取每个通话状态对应的总条数
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/countStatus", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String countStatus(@RequestBody @Valid CallTaskLogCountDTO callTaskLogCountDTO, BindingResult br) {
        XbbResponse<CallTaskLogCountVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.xbbCallTaskLogService.countStatusByTaskId(callTaskLogCountDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取剩余批量外呼条数
     *
     * @return 获取剩余批量外呼条数
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/getRemain", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getRemain(@RequestBody @Valid FeeRemainDTO feeRemainDTO, BindingResult br) {
        XbbResponse<FeeRemainVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FeeRemainVO feeRemainVO = this.xbbCallTaskLogService.getRemain(feeRemainDTO);
                response = new XbbResponse<>(feeRemainVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 客户状态筛选
     *
     * @return 客户状态筛选
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/searchItems", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String searchItems(@RequestBody @Valid CallSearchItemsDTO callSearchItemsDTO, BindingResult br) {
        XbbResponse<CallSearchItemsVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.xbbCallTaskLogService.searchItems(callSearchItemsDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
